﻿namespace Amazon.Checkout.Controllers
{
    using System.Linq;
    using System.Web.Mvc;
    using Models;
    using Orchard;
    using Orchard.ContentManagement;
    using Orchard.Localization;
    using Orchard.Mvc.Extensions;
    using Orchard.UI.Notify;
    using Services;
    using ViewModels;

    public class AdminController : Controller
    {
        private readonly IOrchardServices services;
        private readonly ICheckoutByAmazonService amazonService;

        public AdminController(
                                IOrchardServices services,
                                ICheckoutByAmazonService amazonService)
        {
            this.services = services;
            this.amazonService = amazonService;
            this.T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public ActionResult Settings()
        {
            var settings = this.services.WorkContext.CurrentSite.As<SiteSettingsPart>();

            var promotions = this.amazonService.GetPromotions().ToList();
            promotions.Insert(0, new PromotionRecord { Id = -1, PromotionCode = this.T("-- Select a Promotion --").Text });

            var viewModel = new SiteSettingsViewModel
            {
                MerchantId = settings.MerchantId,
                AwsAccessKeyId = settings.AwsAccessKeyId,
                SecretKeyId = settings.SecretKeyId,
                AssociateTag = settings.AssociateTag,
                PromotionId = settings.PromotionId,
                TaxId = settings.TaxId,
                CurrencyCode = settings.CurrencyCode,
                LengthUnitOfMeasure = settings.LengthUnitOfMeasure,
                VolumeUnitOfMeasure = settings.VolumeUnitOfMeasure,
                WeightUnitOfMeasure = settings.WeightUnitOfMeasure,
                JewelryWeightUnitOfMeasure = settings.JewelryWeightUnitOfMeasure,
                DegreeUnitOfMeasure = settings.DegreeUnitOfMeasure,
                MemorySizeUnitOfMeasure = settings.MemorySizeUnitOfMeasure,
                FrequencyUnitOfMeasure = settings.FrequencyUnitOfMeasure,
                AmperageUnitOfMeasure = settings.AmperageUnitOfMeasure,
                TimeUnitOfMeasure = settings.TimeUnitOfMeasure,
                DateUnitOfMeasure = settings.DateUnitOfMeasure,
                AssemblyTimeUnitOfMeasure = settings.AssemblyTimeUnitOfMeasure,
                AgeRecommendedUnitOfMeasure = settings.AgeRecommendedUnitOfMeasure,
                BatteryPowerUnitOfMeasure = settings.BatteryPowerUnitOfMeasure,
                VoltageUnitOfMeasure = settings.VoltageUnitOfMeasure,
                WattageUnitOfMeasure = settings.WattageUnitOfMeasure,
                TemperatureRatingUnitOfMeasure = settings.TemperatureRatingUnitOfMeasure,
                ClothingSizeUnitOfMeasure = settings.ClothingSizeUnitOfMeasure,

                Promotions = promotions,
            };

            return View(viewModel);
        }

        public ActionResult SaveSettings(string returnUrl)
        {
            var viewModel = new SiteSettingsViewModel();
            TryUpdateModel(viewModel);

            if (ModelState.IsValid)
            {
                var settings = this.services.WorkContext.CurrentSite.As<SiteSettingsPart>();

                settings.MerchantId = viewModel.MerchantId;
                settings.AwsAccessKeyId = viewModel.AwsAccessKeyId;
                settings.SecretKeyId = viewModel.SecretKeyId;
                settings.AssociateTag = viewModel.AssociateTag;
                settings.PromotionId = viewModel.PromotionId;
                settings.TaxId = viewModel.TaxId;
                settings.CurrencyCode = viewModel.CurrencyCode;
                settings.LengthUnitOfMeasure = viewModel.LengthUnitOfMeasure;
                settings.VolumeUnitOfMeasure = viewModel.VolumeUnitOfMeasure;
                settings.WeightUnitOfMeasure = viewModel.WeightUnitOfMeasure;
                settings.JewelryWeightUnitOfMeasure = viewModel.JewelryWeightUnitOfMeasure;
                settings.DegreeUnitOfMeasure = viewModel.DegreeUnitOfMeasure;
                settings.MemorySizeUnitOfMeasure = viewModel.MemorySizeUnitOfMeasure;
                settings.FrequencyUnitOfMeasure = viewModel.FrequencyUnitOfMeasure;
                settings.AmperageUnitOfMeasure = viewModel.AmperageUnitOfMeasure;
                settings.TimeUnitOfMeasure = viewModel.TimeUnitOfMeasure;
                settings.DateUnitOfMeasure = settings.DateUnitOfMeasure;
                settings.AssemblyTimeUnitOfMeasure = viewModel.AssemblyTimeUnitOfMeasure;
                settings.AgeRecommendedUnitOfMeasure = viewModel.AgeRecommendedUnitOfMeasure;
                settings.BatteryPowerUnitOfMeasure = viewModel.BatteryPowerUnitOfMeasure;
                settings.VoltageUnitOfMeasure = viewModel.VoltageUnitOfMeasure;
                settings.WattageUnitOfMeasure = viewModel.WattageUnitOfMeasure;
                settings.TemperatureRatingUnitOfMeasure = viewModel.TemperatureRatingUnitOfMeasure;
                settings.ClothingSizeUnitOfMeasure = viewModel.ClothingSizeUnitOfMeasure;
            }
            else
            {
                foreach (var error in ModelState.Values.SelectMany(m => m.Errors).Select(e => e.ErrorMessage))
                {
                    this.services.Notifier.Error(this.T(error));
                }
            }

            return this.RedirectLocal(returnUrl, "~/");
        }
    }
}